import { promises as fs } from 'fs';
import path from 'path';

import { privacyPolicies } from '@/app/pd-privacy/config';

export async function getPrivacyPolicies() {
    return privacyPolicies;
}

export async function getPrivacyPolicyBySlug(slug: string) {
    const policies = await getPrivacyPolicies();
    const policy = policies.find((p) => p.slug === slug);
    if (!policy) return null;

    try {
        const relativePath = policy.content.replace(/^\//, '');
        const filePath = path.join(process.cwd(), 'public', relativePath);
        const content = await fs.readFile(filePath, 'utf-8');

        return { ...policy, content };
    } catch (error) {
        console.error('Error reading privacy policy content:', error);
        return { ...policy, content: '' };
    }
}
